package cn.qitom.common.launch;

import cn.qitom.common.constant.SystemRunConstant;
import cn.qitom.core.auto.service.AutoService;
import cn.qitom.core.launch.LegoLoadService;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.util.StringUtils;

import java.util.Properties;


/**
 * 每个服务都会用到的通用配置，nacos,seata,zipkin 等
 */
@AutoService(LegoLoadService.class)
public class LegoLauncher implements LegoLoadService {



    public static void setProperty(Properties props, String key, String value) {
        if (StringUtils.isEmpty(props.getProperty(key))) {
            props.setProperty(key, value);
        }

    }

    @Override
    public void init(SpringApplicationBuilder builder, String appName, String profile) {
        Properties props = System.getProperties();
        setProperty(props, "lego.env",profile);
        setProperty(props, "spring.cloud.nacos.discovery.server-addr", SystemRunConstant.nacosAddr(profile));
        // setProperty(props, "spring.cloud.nacos.discovery.namespace", SystemRunConstant.nacosNamespace(profile));
        setProperty(props, "spring.cloud.nacos.config.server-addr", SystemRunConstant.nacosAddr(profile));
        // setProperty(props, "spring.cloud.nacos.config.namespace", SystemRunConstant.nacosNamespace(profile));
    }

    @Override
    public int getOrder() {
        return 0;
    }
}
